<?php

class DealAction extends CommonAction{
	public function online_index()
	{		
		if(trim($_REQUEST['name'])!='')
		{
			$map['name'] = array('like','%'.trim($_REQUEST['name']).'%');
		}
		
		if(intval($_REQUEST['time_status'])==1)
		{
			$map['_string'] = '(begin_time > '.get_gmtime().')';			
		}
		if(intval($_REQUEST['time_status'])==2)
		{
			$map['_string'] = "(begin_time < '".get_gmtime()."') and ((end_time > '".get_gmtime()."') or (end_time = 0))";
		}
		
		if(intval($_REQUEST['time_status'])==3)
		{
			$map['_string'] = '(end_time < '.get_gmtime().') and (end_time <> 0)';	
		}
		
		if(intval($_REQUEST['cate_id'])>0)
		{
			$map['cate_id'] = intval($_REQUEST['cate_id']);
		}
		
		if(intval($_REQUEST['user_id'])>0)
		{
			$map['user_id'] = intval($_REQUEST['user_id']);
		}
		
		$map['is_effect'] = 1;		
		$map['is_delete'] = 0;		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		
		$cate_list = M("DealCate")->select();
		$this->assign("cate_list",$cate_list);
		$this->display ();
	}
	
	public function submit_index()
	{
		if(trim($_REQUEST['name'])!='')
		{
			$map['name'] = array('like','%'.trim($_REQUEST['name']).'%');
		}
		
		if(intval($_REQUEST['cate_id'])>0)
		{
			$map['cate_id'] = intval($_REQUEST['cate_id']);
		}
		
		if(intval($_REQUEST['user_id'])>0)
		{
			$map['user_id'] = intval($_REQUEST['user_id']);
		}
		
		$map['is_effect'] = 0;	
		$map['is_delete'] = 0;		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		
		$cate_list = M("DealCate")->select();
		$this->assign("cate_list",$cate_list);
		$this->display ();
	}
	
	public function delete_index()
	{
		if(trim($_REQUEST['name'])!='')
		{
			$map['name'] = array('like','%'.trim($_REQUEST['name']).'%');
		}
		
		if(intval($_REQUEST['cate_id'])>0)
		{
			$map['cate_id'] = intval($_REQUEST['cate_id']);
		}
		
		if(intval($_REQUEST['user_id'])>0)
		{
			$map['user_id'] = intval($_REQUEST['user_id']);
		}
		

		$map['is_delete'] = 1;		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		
		$cate_list = M("DealCate")->select();
		$this->assign("cate_list",$cate_list);
		$this->display ();
	}
	
	public function add()
	{
		$cate_list = M("DealCate")->select();
		$this->assign("cate_list",$cate_list);
		
		$region_lv2 = M('region_conf')->where(array('region_level'=>'2'))->order('py asc')->select();
		$this->assign("region_lv2",$region_lv2);			
		
		$this->assign("new_sort", M("Deal")->max("sort")+1);
		$this->display();
	}
	public function edit() {		
		$id = intval($_REQUEST ['id']);
		$condition['id'] = $id;		
		$vo = M(MODULE_NAME)->where($condition)->find();
        $vo['description'] = stripslashes($vo['description']);
        $vo['description'] = str_replace('./','/',$vo['description']);
		if($vo['user_id']==0)$vo['user_id']  = '';
		$vo['begin_time'] = $vo['begin_time']!=0?to_date($vo['begin_time']):'';
		$vo['end_time'] = $vo['end_time']!=0?to_date($vo['end_time']):'';
		$this->assign ( 'vo', $vo );
		
		$cate_list = M("DealCate")->select();
		$this->assign("cate_list",$cate_list);
		
		$region_pid = 0;
		$region_lv2 = M('region_conf')->where(array('region_level'=>'2'))->order('py asc')->select();
		foreach($region_lv2 as $k=>$v)
		{
			if($v['name'] == $vo['province'])
			{
				$region_lv2[$k]['selected'] = 1;
				$region_pid = $region_lv2[$k]['id'];
				break;
			}
		}
		$this->assign("region_lv2",$region_lv2);
		
		
		if($region_pid>0)
		{
			$region_lv3 = M('region_conf')->where(array('pid'=>(string)$region_pid))->order('py asc')->select();
			foreach($region_lv3 as $k=>$v)
			{
				if($v['name'] == $vo['city'])
				{
					$region_lv3[$k]['selected'] = 1;
					break;
				}
			}
			$this->assign("region_lv3",$region_lv3);
		}
		
		$qa_list = M("DealFaq")->where("deal_id=".$vo['id'])->order("sort asc")->select();
		$this->assign("faq_list",$qa_list);
		
		$this->display ();
	}


    public function redound_index(){
        $model = M('deal_item');
        $current_time = date('Y-m-d');
        if(trim($_REQUEST['name'])!='')
        {
            $map['b.name'] = array('like','%'.trim($_REQUEST['name']).'%');
        }

        if(intval($_REQUEST['cate_id'])>0)
        {
            $map['b.cate_id'] = intval($_REQUEST['cate_id']);
        }

        if(intval($_REQUEST['user_id'])>0)
        {
            $map['b.user_id'] = intval($_REQUEST['user_id']);
        }
        $remain_redound = intval($_REQUEST['remain_redound']) <= 0 ? 1 : intval($_REQUEST['remain_redound']);
        $map['a.support_count'] = array('GT','0');
        $map['b.is_effect'] = '1';
        $map['b.is_delete'] = '0';
        $map['b.is_success'] = '1';
        $map['b.end_time'] = array('NEQ','0');
        $map['_string'] = "DATEDIFF('{$current_time}',FROM_UNIXTIME(b.end_time,'%Y-%m-%d')) - (a.repaid_day - $remain_redound) = '2'";
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }
        $sortBy = '';
        $asc = 'desc';
        $field_list = $model->getDbFields();
        //排序字段 默认为主键名
        if (isset ( $_REQUEST ['_order'] )) {
            $field_list = $model->getDbFields();
            if(in_array($_REQUEST['_order'],$field_list)){
                $order = 'a.'.$_REQUEST ['_order'];
            }else{
                $order = 'b.'.$_REQUEST ['_order'];
            }

        } else {
            $order = ! empty ( $sortBy ) ? $sortBy : 'a.'.$model->getPk ();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if (isset ( $_REQUEST ['_sort'] )) {

            $sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        $count = $model->alias('a')->join('__DEAL__ b on a.deal_id = b.id')->where ( $map )->count ( 'a.id' );
        //echo $model->_sql();
        if ($count > 0) {
            //创建分页对象
            if (! empty ( $_REQUEST ['listRows'] )) {
                $listRows = $_REQUEST ['listRows'];
            } else {
                $listRows = '';
            }
            import('ORG.Util.Page');
            $p = new Page ( $count, $listRows );
            //分页查询数据

            $voList = $model->alias('a')->join('__DEAL__ b on a.deal_id = b.id')->where($map)->field('a.*,b.id,b.name,b.end_time,b.user_name')->order("b.end_time asc,a.repaid_day asc")->order("{$order} {$sort}")->limit($p->firstRow . ',' . $p->listRows)->select ( );
            //分页跳转的时候保证查询条件
            $request_data = array();
            $request_data = array_replace_recursive($_GET,$_POST);
            $p->parameter = http_build_query($request_data);
            //分页显示

            $page = $p->show ();
            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式
            //模板赋值显示
            $this->assign ( 'list', $voList );
            $this->assign ( 'sort', $sort );
            $this->assign ( 'order', $order );
            $this->assign ( 'sortImg', $sortImg );
            $this->assign ( 'sortType', $sortAlt );
            $this->assign ( "page", $page );
            $this->assign ( "nowPage",I('page') ? I('page') : 1);
        }

        $cate_list = M("DealCate")->select();
        $this->assign("cate_list",$cate_list);
        $this->display();
    }
	
	
	
	
	public function insert() {
		B('FilterString');
		$ajax = intval($_REQUEST['ajax']);
		if(empty($_REQUEST['user_id'])){
			$this->error("请输入发起人ID");
		}
		$data = M(MODULE_NAME)->create ();
		//开始验证有效性
		$this->assign("jumpUrl",u(MODULE_NAME."/add"));
		if(!check_empty($data['name']))
		{
			$this->error("请输入名称");
		}	
		
		if(intval($data['cate_id'])==0)
		{
			$this->error("请选择分类");
		}

        if(intval($data['deal_days']) <= 0){
            $this->error("参考上线天数不能小于1");
        }

        if(empty($data['begin_time']) || strtotime($data['begin_time']) == false){
            $this->error("开始时间填写错误");
        }


		$data['begin_time'] = trim($data['begin_time'])==''?0:to_timespan($data['begin_time']);
        $data['end_time'] = strtotime("+{$data['deal_days']} days",$data['begin_time']) ;
		$data['create_time'] = get_gmtime();
		$data['user_name'] = M("User")->where(array("id"=>intval($data['user_id'])))->getField("user_name");
		
		if($data['video']!="")
		{
			require_once SITE_PATH."system/utils/video.php";
			$video = fetch_video_url($data['video']);
			if($video!="")
			{
				$data['source_video'] =  $video;
			}
			else
			{
				$this->error("非法的视频地址");
			}
		}
		
		// 更新数据
		$log_info = $data['name'];
		$list=M(MODULE_NAME)->add($data);
		if (false !== $list) {
			//成功提示
			
			if($data['is_effect']==1&&$data['user_id']>0)
			{
				$deal_count = M("Deal")->where("user_id=".$data['user_id']." and is_effect = 1 and is_delete = 0")->count();
				M("User")->where("id=".$data['user_id'])->setField("build_count",$deal_count);
			}
			
			foreach($_REQUEST['question'] as $k=>$v)
			{
				if(trim($v)!=""||trim($_REQUEST['answer'][$k])!='')
				{
					$qa = array();
					$qa['deal_id'] = $list;
					$qa['question'] = trim($v);
					$qa['answer'] = trim($_REQUEST['answer'][$k]);
					$qa['sort'] = intval($k)+1;
					M("DealFaq")->add($qa);
				}
			}
			
			syn_deal($list);
			save_log($log_info.L("INSERT_SUCCESS"),1);
			$this->success(L("INSERT_SUCCESS"));
		} else {
			//错误提示
			save_log($log_info.L("INSERT_FAILED"),0);
			$this->error(L("INSERT_FAILED").M(MODULE_NAME)->_sql());
		}
	}	
	
	public function update() {
		B('FilterString');
		$data = M(MODULE_NAME)->create ();
		
		$log_info = M(MODULE_NAME)->where(array("id"=>intval($data['id'])))->getField("name");
		//开始验证有效性
		$this->assign("jumpUrl",u(MODULE_NAME."/edit",array("id"=>$data['id'])));
		if(!check_empty($data['name']))
		{
			$this->error("请输入名称");
		}	
		if(intval($data['cate_id'])==0)
		{
			$this->error("请选择分类");
		}

        if(intval($data['deal_days']) <= 0){
            $this->error("参考上线天数不能小于1");
        }

        if(empty($data['begin_time']) || strtotime($data['begin_time']) == false){
            $this->error("开始时间填写错误");
        }


        $data['begin_time'] = trim($data['begin_time'])==''?0:to_timespan($data['begin_time']);
        $data['end_time'] = strtotime("+{$data['deal_days']} days",$data['begin_time']) ;
		$data['create_time'] = get_gmtime();
		$data['user_name'] = M("User")->where(array("id"=>intval($data['user_id'])))->getField("user_name");

		if(!empty($data['video']))
		{
            require_once SITE_PATH . "system/utils/video.php";
			$video = fetch_video_url($data['video']);
			if($video!="")
			{
				$data['source_video'] =  $video;
			}
			else
			{
				$this->error("非法的视频地址");
			}
		}
		else
		{
			$data['source_video'] = "";
		}
		$list=M(MODULE_NAME)->data($data)->save();
		if (false !== $list) {
			if($data['is_effect']==1&&$data['user_id']>0)
			{
				$deal_count = M("Deal")->where(array("user_id"=>$data['user_id'],"is_effect" => '1',"is_delete" => '0'))->count();
				M("User")->where(array("id"=>$data['user_id']))->setField("build_count",$deal_count);
			}
			//成功提示			
			M("DealFaq")->where(array("deal_id"=>$data['id']))->delete();
			foreach($_REQUEST['question'] as $k=>$v)
			{
				if(trim($v)!=""||trim($_REQUEST['answer'][$k])!='')
				{
					$qa = array();
					$qa['deal_id'] = $data['id'];
					$qa['question'] = trim($v);
					$qa['answer'] = trim($_REQUEST['answer'][$k]);
					$qa['sort'] = intval($k)+1;
					M("DealFaq")->add($qa);
				}
			}
			
			syn_deal($data['id']);
			save_log($log_info.L("UPDATE_SUCCESS"),1);
			$this->success(L("UPDATE_SUCCESS"));
		} else {
			//错误提示
			save_log($log_info.L("UPDATE_FAILED"),0);
			$this->error(L("UPDATE_FAILED"),0,$log_info.L("UPDATE_FAILED"));
		}
	}
	
	public function set_sort()
	{
		$id = intval($_REQUEST['id']);
		$sort = intval($_REQUEST['sort']);
		$log_info = M("Deal")->where("id=".$id)->getField("name");
		if(!check_sort($sort))
		{
			$this->error(l("SORT_FAILED"),1);
		}
		M("Deal")->where("id=".$id)->setField("sort",$sort);
		save_log($log_info.l("SORT_SUCCESS"),1);
		$this->success(l("SORT_SUCCESS"),1);
	}
	
	public function delete() {
		//彻底删除指定记录
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );
				$rel_data = M(MODULE_NAME)->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$info[] = $data['name'];	
				}
				if($info) $info = implode(",",$info);
				$list = M(MODULE_NAME)->where ( $condition )->setField("is_delete",1);		
						
				if ($list!==false) {
					foreach($rel_data as $data)
					{						
						$deal_count = M("Deal")->where("user_id=".$data['user_id']." and is_effect = 1 and is_delete = 0")->count();
						M("User")->where("id=".$data['user_id'])->setField("build_count",$deal_count);						
					}
					save_log($info."成功移到回收站",1);
					$this->success ("成功移到回收站",$ajax);
				} else {
					save_log($info."移到回收站出错",0);					
					$this->error ("移到回收站出错",$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	public function restore() {
		//彻底删除指定记录
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );
				$rel_data = M(MODULE_NAME)->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$info[] = $data['name'];	
				}
				if($info) $info = implode(",",$info);
				$list = M(MODULE_NAME)->where ( $condition )->setField("is_delete",0);				
				if ($list!==false) {
					save_log($info."恢复成功",1);
					$this->success ("恢复成功",$ajax);
				} else {
					save_log($info."恢复出错",0);
					$this->error ("恢复出错",$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	public function foreverdelete() {
		//彻底删除指定记录
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );
				$link_condition = array ('deal_id' => array ('in', explode ( ',', $id ) ) );
				$rel_data = M(MODULE_NAME)->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$info[] = $data['name'];	
				}
				if($info) $info = implode(",",$info);
				$list = M(MODULE_NAME)->where ( $condition )->delete();				
				if ($list!==false) {					
					M("DealFaq")->where($link_condition)->delete();
					M("DealComment")->where($link_condition)->delete();
					M("DealFocusLog")->where($link_condition)->delete();
					M("DealItem")->where($link_condition)->delete();
					M("DealItemImage")->where($link_condition)->delete();
					M("DealOrder")->where($link_condition)->delete();
					M("DealPayLog")->where($link_condition)->delete();
					M("DealSupportLog")->where($link_condition)->delete();
					M("DealVisitLog")->where($link_condition)->delete();
					M("DealLog")->where($link_condition)->delete();
					M("UserDealNotify")->where($link_condition)->delete();
					M("DealNotify")->where($link_condition)->delete();
					
					save_log($info.l("FOREVER_DELETE_SUCCESS"),1);
					$this->success (l("FOREVER_DELETE_SUCCESS"),$ajax);
				} else {
					save_log($info.l("FOREVER_DELETE_FAILED"),0);
					$this->error (l("FOREVER_DELETE_FAILED"),$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	public function add_faq()
	{
		$this->display();
	}
	
	public function deal_item()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		$this->assign("deal_info",$deal_info);
		
		if($deal_info)
		{
			$map['deal_id'] = $deal_info['id'];		
			if (method_exists ( $this, '_filter' )) {
				$this->_filter ( $map );
			}
			$name=$this->getActionName();
			$model = D ("DealItem");
			if (! empty ( $model )) {
				$this->_list ( $model, $map );
			}
		}
		
		$this->display();
	}
	
	public function add_deal_item()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		$this->assign("deal_info",$deal_info);
		$this->display();
	}
	
	
	public function insert_deal_item() {
		B('FilterString');
		$ajax = intval($_REQUEST['ajax']);
		$data = M("DealItem")->create ();

		//开始验证有效性
		$this->assign("jumpUrl",u(MODULE_NAME."/add_deal_item",array("id"=>$data['deal_id'])));
		if(!check_empty($data['price']))
		{
			$this->error("请输入价格");
		}	
		
		// 更新数据
		
		$list=M("DealItem")->add($data);
		$log_info =  "项目ID".$data['deal_id'].":".format_price($data['price']);	
		if (false !== $list) {
			//成功提示
			
			$imgs = $_REQUEST['image'];
			foreach($imgs as $k=>$v)
			{
				if($v!='')
				{
					$img_data['deal_id'] = $data['deal_id'];
					$img_data['deal_item_id'] = $list;
					$img_data['image'] = $v;
					M("DealItemImage")->add($img_data);
				}
			}
			
			save_log($log_info.L("INSERT_SUCCESS"),1);
			$this->success(L("INSERT_SUCCESS"));
		} else {
			//错误提示
			save_log($log_info.L("INSERT_FAILED"),0);
			$this->error(L("INSERT_FAILED"));
		}
	}
	
	public function edit_deal_item()
	{
		$id = intval($_REQUEST ['id']);
		$condition['id'] = $id;		
		$vo = M("DealItem")->where($condition)->find();
		$this->assign ( 'vo', $vo );
		
		//输出图片集
		$img_list = M("DealItemImage")->where(array("deal_item_id"=>$vo['id']))->select();
		$imgs = array();
		foreach($img_list as $k=>$v)
		{
			$imgs[$k] = $v['image']; 
		}
		$this->assign("img_list",$imgs);
		
		$this->display();
	}
	
	public function update_deal_item() {
		B('FilterString');
		$ajax = intval($_REQUEST['ajax']);
		$data = M("DealItem")->create ();

		//开始验证有效性
		$this->assign("jumpUrl",u(MODULE_NAME."/edit_deal_item",array("id"=>$data['id'])));
		if(!check_empty($data['price']))
		{
			$this->error("请输入价格");
		}	
		
		// 更新数据
		
		$list=M("DealItem")->save($data);
		$log_info =  "项目ID".$data['deal_id'].":".format_price($data['price']);	
		if (false !== $list) {
			//成功提示
			//开始处理图片
			M("DealItemImage")->where(array("deal_item_id"=>$data['id']))->delete();
			$imgs = $_REQUEST['image'];
			foreach($imgs as $k=>$v)
			{
				if($v!='')
				{
					$img_data['deal_item_id'] = $data['id'];
					$img_data['deal_id'] = $data['deal_id'];
					$img_data['image'] = $v;
					M("DealItemImage")->add($img_data);
				}
			}
			//end 处理图片
			save_log($log_info.L("UPDATE_SUCCESS"),1);
			$this->success(L("UPDATE_SUCCESS"));
		} else {
			//错误提示
			save_log($log_info.L("UPDATE_FAILED"),0);
			$this->error(L("UPDATE_FAILED"));
		}
	}
	
	public function del_deal_item()
	{
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );				
				$rel_data = M("DealItem")->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$deal_id = $data['deal_id'];
					$info[] = format_price($data['price']);	
				}
				if($info) $info = implode(",",$info);
				$info = "项目ID".$deal_id.":".$info;
				$list = M("DealItem")->where ( $condition )->delete();				
				if ($list!==false) {					
					
					syn_deal($deal_id);
					save_log($info.l("FOREVER_DELETE_SUCCESS"),1);
					$this->success (l("FOREVER_DELETE_SUCCESS"),$ajax);
				} else {
					save_log($info.l("FOREVER_DELETE_FAILED"),0);
					$this->error (l("FOREVER_DELETE_FAILED"),$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	
	
	//pay_log 放款日志
	public function pay_log()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		$deal_info['commission'] = $deal_info['support_amount'] - $deal_info['pay_amount'];
		$this->assign("deal_info",$deal_info);
		
		if($deal_info)
		{
			$map['deal_id'] = $deal_info['id'];		

			$model = D ("DealPayLog");
			$paid_money = $model->where($map)->sum("money");
			$remain_money = $deal_info['pay_amount'] - $paid_money;
			$this->assign("remain_money",$remain_money);
			$this->assign("paid_money",$paid_money);
			if (! empty ( $model )) {
				$this->_list ( $model, $map );
			}
		}
		
		$this->display();
	}
	
	public function add_pay_log()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		$deal_info['commission'] = $deal_info['support_amount'] - $deal_info['pay_amount'];
		$this->assign("deal_info",$deal_info);
		
		if($deal_info)
		{
			$map['deal_id'] = $deal_info['id'];		
	
			$model = D ("DealPayLog");
			$paid_money = $model->where($map)->sum("money");
			$remain_money = $deal_info['pay_amount'] - $paid_money;
			$this->assign("paid_money",$paid_money);
			$this->assign("remain_money",$remain_money);
		}
		
		$this->display();
	}
	
	public function save_pay_log()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		
		
		if($deal_info)
		{
			$map['deal_id'] = $deal_info['id'];		
		
			$model = D ("DealPayLog");
			$paid_money = $model->where($map)->sum("money");
			$remain_money = $deal_info['pay_amount'] - $paid_money;
			
			$money = doubleval($_REQUEST['money']);
			$log_info = strim($_REQUEST['log_info']);
			
			if($money<=0||$money>$remain_money)
			{
				$this->error("金额出错");
			}
			else
			{
				if($deal_info['user_id']>0)
				{
					require_once SITE_PATH."system/libs/user.php";
					if($log_info=="")$log_info = $deal_info['name']."项目筹款发放";
					modify_account(array("money"=>$money),$deal_info['user_id'],$log_info);
					$log['deal_id'] = $deal_info['id'];
					$log['money'] = $money;
					$log['create_time'] = get_gmtime();
					$log['log_info'] = $log_info;
					$model->add($log);
					save_log($log_info.$money,1);
					$this->success("筹款发放成功");
				}
				else
				{
					$this->error("管理员创建项目，无需发放筹款");
				}
			}
			
		}
		else
		{
			$this->error("项目不存在");
		}
	}
	
	public function del_pay_log()
	{
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );				
				$rel_data = M("DealPayLog")->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$deal_id = $data['deal_id'];
					$info[] = format_price($data['money']);	
				}
				if($info) $info = implode(",",$info);
				$info = "项目ID".$deal_id.":".$info;
				$list = M("DealPayLog")->where ( $condition )->delete();				
				if ($list!==false) {					
					
					save_log($info.l("FOREVER_DELETE_SUCCESS"),1);
					$this->success (l("FOREVER_DELETE_SUCCESS"),$ajax);
				} else {
					save_log($info.l("FOREVER_DELETE_FAILED"),0);
					$this->error (l("FOREVER_DELETE_FAILED"),$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	//项目日志
	public function deal_log()
	{
		$deal_id = intval($_REQUEST['id']);
		$deal_info = M("Deal")->getById($deal_id);
		$this->assign("deal_info",$deal_info);
		
		if($deal_info)
		{
			$map['deal_id'] = $deal_info['id'];	
			$model = D ("DealLog");
			if (! empty ( $model )) {
				$this->_list ( $model, $map );
			}
		}
		
		$this->display();
	}
	
	public function del_deal_log()
	{
		$ajax = intval($_REQUEST['ajax']);
		$id = $_REQUEST ['id'];
		if (isset ( $id )) {
				$condition = array ('id' => array ('in', explode ( ',', $id ) ) );		
				$condition_log = array ('log_id' => array ('in', explode ( ',', $id ) ) );				
				$rel_data = M("DealLog")->where($condition)->select();				
				foreach($rel_data as $data)
				{
					$deal_id = $data['deal_id'];
					$info[] = $data['id'];	
				}
				if($info) $info = implode(",",$info);
				$info = "项目ID".$deal_id."的日志:".$info;
				$list = M("DealLog")->where ( $condition )->delete();	
							
				if ($list!==false) {		
					M('deal')->where(array('id'=>(string)$deal_id))->setDec('log_count',intval($list));
					M("DealComment")->where($condition_log)->delete();
					save_log($info.l("FOREVER_DELETE_SUCCESS"),1);
					$this->success (l("FOREVER_DELETE_SUCCESS"),$ajax);
				} else {
					save_log($info.l("FOREVER_DELETE_FAILED"),0);
					$this->error (l("FOREVER_DELETE_FAILED"),$ajax);
				}
			} else {
				$this->error (l("INVALID_OPERATION"),$ajax);
		}
	}
	
	public function batch_refund()
	{
		$page = intval($_REQUEST['page']);

		$page=($page<=0)?1:$page;

		$page_size = 100;
		$deal_id = intval($_REQUEST['id']);
		
		$limit = (($page-1)*$page_size).",".$page_size;
		
		$deal_info = M("Deal")->where("id=".$deal_id." and is_delete = 0 and is_effect = 1 and is_success = 0 and end_time <>0 and end_time <".get_gmtime())->find();
		if(!$deal_info)
		{
			$this->error("该项目不能批量退款");
		}
		else
		{
			require_once SITE_PATH."system/libs/user.php";
			$refund_order_list = M("DealOrder")->where("deal_id=".$deal_id." and is_refund = 0 and order_status = 3")->limit($limit)->select();
			foreach($refund_order_list as $k=>$v)
			{
				$affected_rows = M('deal_order')->where(array('id'=>$v['id']))->data(array('is_refund'=>'1'))->save();
				if($affected_rows>0)
				{
					modify_account(array("money"=>$v['total_price']),$v['user_id'],$v['deal_name']."项目失败退款");					
				}
			}
			$remain = M("DealOrder")->where("deal_id=".$deal_id." and is_refund = 0 and order_status = 3")->count();
			if($remain==0)
			{
				$jump_url = u("Deal/online_index");
				$this->assign("jumpUrl",$jump_url);
				$this->success("批量退款成功");
			}
			else
			{
				$jump_url = u("Deal/batch_refund",array("id"=>$deal_id,"page"=>$page+1));
				$this->assign("jumpUrl",$jump_url);
				$this->success("批量退款中，请勿刷新页面，剩余".$remain."条订单未退款");
			}
			
		}
		
	}

    /**
     * 发送邮件
     */
    function send_mail(){
        $deal_id = $_REQUEST['id'];
        $deal_info = M('deal')->where(array('id'=>$deal_id))->find();
        if(!$deal_info){
            exit('找不到该众筹项目');
        }
        if($deal_info['end_time'] > time()){
            exit('该项目目前仍然在众筹当中，不能群发邮件通知');
        }
        $tpl_name = 'TPL_USER_PROJECT_UNSUCCESS';
        $title = '您参与的众筹项目没有成功';
        if($deal_info['end_time'] < time() && $deal_info['is_success'] == '1'){
            //众筹成功
            $tpl_name = 'TPL_USER_PROJECT_SUCCESS';
            $title = '您参与的项目众筹成功';
        }

        $tmpl = M('msg_template')->where(array('name'=>$tpl_name))->find();
        if($tmpl){
            $tmpl_content=  $tmpl['content'];
            $content_data = $tmpl_content;
            $data_info = array('project'=>$deal_info['name']);
            foreach($data_info as $key=>$value){
                $content_data = str_replace("{\${$key}}",$value,$tmpl_content);
            }
            $_GET['page'] = $_REQUEST['page'] = isset($_GET['page']) ? $_GET['page'] : 1;
            $page_size = 100;//每次发送100条
            $order_total = M('deal_order')->where(array('deal_id'=>$deal_id,'order_status'=>'3'))->count();
            if($order_total == 0){
                exit('该项目没有任何人购买,群发邮件结束');
            }
            $Page = new Page($order_total,$page_size);
            $current_page  = intval($_REQUEST['page']);
            $max_page = $Page->totalPages;
            $send_list = M('deal_order')->where(array('deal_id'=>$deal_id,'order_status'=>'3'))->field('user_name,deal_name')->order('id asc')->limit($Page->firstRow,$Page->listRows)->select();
            $send_email_list = array();
            foreach($send_list as $k=>$v){
                $send_email_list[] = $v['user_name'];
            }
            $result = sendmail_cloud(implode(';',$send_email_list),$title,$content_data,false);
            if($max_page <= $current_page){
                exit('邮件通知全部发送完成');
            }else{
                $next_page = $current_page+1;
                $url = "/admin.php?m=Deal&a=send_mail&id={$deal_id}&page=$next_page";
                redirect($url,2,"总批数:{$max_page} 当前批数:{$current_page} 正在继续发送下一批");
                exit();
            }


        }else{
            exit('模板内容找不到');
        }

    }

}
?>